package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.peakseacraft.PeakSeacraftMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PeakSeacraftRecyleStrategy extends AbstractItemRecycleStrategy {
    @Override
    public void markNeedRecycleItem() {
        // 【南海丝路】
        ActivityInfo activityInfo = PeakSeacraftMgr.getActivityInfo();
        if (activityInfo != null) {
            // 回收捐献道具
            long recycleTime = activityInfo.getEndTime() * 1000L + 300 * 1000;
            ActivityItemRecycleMgr.addRecycleItemData(PeakSeacraftMgr.getConfig().getSEACRAFT_CRAFT_DONATE_ITEMS(), activityType, 1, recycleTime, new ArrayList<>());
            // 回收 火炮 扳手
            ActivityItemRecycleMgr.addRecycleItemData(PeakSeacraftMgr.getConfig().getSEACRAFT_CRAFT_WRENCH_ITEM_ID(), activityType, 2, recycleTime, new ArrayList<>());
            ActivityItemRecycleMgr.addRecycleItemData(PeakSeacraftMgr.getConfig().getSEACRAFT_CRAFT_FIRE_ITEM_ID(), activityType, 2, recycleTime, new ArrayList<>());
            // 回收 船桨
            for (Integer padderId : PeakSeacraftMgr.getConfig().getPaddleItemIdList()) {
                ActivityItemRecycleMgr.addRecycleItemData(padderId, activityType, 2, recycleTime, new ArrayList<>());
            }
            // 回收 航海勋章
            recycleTime = activityInfo.getEndShowTime() * 1000L + 600 * 1000;
            ActivityItemRecycleMgr.addRecycleItemData(PeakSeacraftMgr.getConfig().getSEACRAFT_CRAFT_SCORE_ITEM_ID(), activityType, recycleTime);
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.PeakSeacraftActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int recycleType = data.getRecycleType();
        String language = UserMgr.getUserInfo(userId).getLanguage();
        UserBagItem userBagItem = getUserBagItem(userId, data.getItemId());
        if (userBagItem != null && userBagItem.getNum() > 0) {
            long num = userBagItem.getNum();
            if (removeItem(userBagItem) && recycleType != 0) {
                // 转换为道具
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(data.getItemId());
                Property property = new Property();
                if (recycleType == 1) {
                    // 权重获得
                    for (int i = 0; i < num; i++) {
                        String extendParam = goodsInfo.getExtendParam();
                        property.addProperty(RandomWeightConfigHelper.getRandomProperty(extendParam));
                    }
                } else {
                    // 加宝箱奖励
                    for (int i = 0; i < num; i++) {
                        OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getRandomGoods(goodsInfo.getGoodsId());
                        property.addProperty(openGoodsBagResult.getReward());
                    }
                }
                //  发邮件
                String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                String mailTitle = MultipleLanguageMgr.getContent(MailManager.PEAKSEACRAFT_REMOVE_ITEM_TITLE, language);
                StringBuilder removeStr = new StringBuilder();
                removeStr.append(removeName).append("X").append(num);
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.PEAKSEACRAFT_REMOVE_ITEM_CONTENT,
                        language), removeStr.toString());
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(property), mailContent, mailTitle);
            }
        }
    }


    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
